<?php
namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Employee;
use AuctionCore\System\AuctionException;
use AuctionCore\Fours\Fours;
use AuctionCore\Common\Page;
use AuctionCore\System\SystemLog;
use AuctionCore\Common\Util;

use AuctionBack\Logic\FourStoreLogic;
use AuctionBack\Logic\RegionLogic;
use AuctionBack\Logic\BlocLogic;

/**
 * 供应商管理
 */
class SupplierManageController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
        //分公司
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);

        //判断当前登陆人是否是4s店维护人 片区维护人
        $e = new Employee($this->user_info['username']);
        $is_maintain = $e->isFourStoreMaintainOrDeveloper(2);
        // $is_developer = $e->isFourStoreMaintainOrDeveloper(1);
        $Region = D('Region', 'Logic');
        $regionMess = $Region->getRegionByLeader($this->emp_id);
        $is_leader = !empty($regionMess) ? 1 : 0;
        if ($is_leader) {
            //找到片区下所有维护人
            $resMaintain = $Region->getRegionMaintain($regionMess['rid']);
            $this->assign('resMaintain', $resMaintain);
        }
        $this->assign('is_leader', $is_leader);
        $this->assign('is_maintain', $is_maintain);
        // $this->assign('is_developer',$is_developer);
        if ($is_maintain || $is_leader) {
            //找到当前人所属片区
            $regionEmp = $e->getRegionByEmp();
            $this->assign('regionEmp', $regionEmp);
        }

        if ($regionEmp) {
            $RegionInfo = $Region->detail($regionEmp['region_id']);
            $address = new \AuctionCore\Other\Address();
            $cityEmp = $address->detail($RegionInfo['city']);
            $this->assign('cityEmp', $cityEmp);
            $BranchEmp = $address->getbranchInfoByCity($RegionInfo['city']);
            $this->assign('BranchEmp', $BranchEmp);
        }
    }

    public function index()
    {
        $status = I('get.status/d', 1);
        $this->assign('status', $status);
        //片区管理权限
        $reginRules = "PianQuGuanLi";
        $e = new Employee($this->user_info['username']);
        $reginAuth = $e->check_auth($reginRules);
        $reginAuth = $reginAuth ? 1 : 0;
        $this->assign('reginAuth', $reginAuth);
        //4s店添加
        $foursAddRules = "4STianJia";
        $foursAddAuth = $e->check_auth($foursAddRules);
        $foursAddAuth = $foursAddAuth ? 1 : 0;
        $this->assign('foursAddAuth', $foursAddAuth);

        //集团添加
        $blocAddRules = "JiTuanTianJia";
        $blocAddAuth = $e->check_auth($blocAddRules);
        $blocAddAuth = $blocAddAuth ? 1 : 0;
        $this->assign('blocAddAuth', $blocAddAuth);

        //片区添加
        $reginAddRules = "PianQuTianJia";
        $reginAddAuth = $e->check_auth($reginAddRules);
        $reginAddAuth = $reginAddAuth ? 1 : 0;
        $this->assign('reginAddAuth', $reginAddAuth);

        $this->display();
    }


    public function get_list()
    {
        $p = I('request.p/d', 1);
        $p = $p < 2 ? 1 : $p;
        $limit = 10;
        //搜索条件
        $params['status'] = I('get.status', '');
        $e = new Employee($this->user_info['username']);
        //4s店修改
        $foursEidtRules = "4SXiuGai";
        $foursEidtAuth = $e->check_auth($foursEidtRules);
        $foursEidtAuth = $foursEidtAuth ? 1 : 0;
        $this->assign('foursEidtAuth', $foursEidtAuth);
        //4s店修改（高级）
        $foursTopEidtRules = "4SGaoJiXiuGai";
        $foursTopEidtAuth = $e->check_auth($foursTopEidtRules);
        $foursTopEidtAuth = $foursTopEidtAuth ? 1 : 0;
        $this->assign('foursTopEidtAuth', $foursTopEidtAuth);
        //4s店关联评估师
        $foursAppraiserRules = "4SGuanLianPingGuShi";
        $foursAppraiserAuth = $e->check_auth($foursAppraiserRules);
        $foursAppraiserAuth = $foursAppraiserAuth ? 1 : 0;
        $this->assign('foursAppraiserAuth', $foursAppraiserAuth);

        //集团修改
        $blocEidtRules = "JiTuanXiuGai";
        $blocEidtAuth = $e->check_auth($blocEidtRules);
        $blocEidtAuth = $blocEidtAuth ? 1 : 0;
        $this->assign('blocEidtAuth', $blocEidtAuth);
        //集团关联店铺
        $blocFoursRules = "JiTuanGuanLianDianPu";
        $blocFoursAuth = $e->check_auth($blocFoursRules);
        $blocFoursAuth = $blocFoursAuth ? 1 : 0;
        $this->assign('blocFoursAuth', $blocFoursAuth);

        //片区修改
        $reginEidtRules = "PianQuXiuGai";
        $reginEidtAuth = $e->check_auth($reginEidtRules);
        $reginEidtAuth = $reginEidtAuth ? 1 : 0;
        $this->assign('reginEidtAuth', $reginEidtAuth);
        //片区关联店铺
        $reginFoursRules = "PianQuGuanLianDianPu";
        $reginFoursAuth = $e->check_auth($reginFoursRules);
        $reginFoursAuth = $reginFoursAuth ? 1 : 0;
        $this->assign('reginFoursAuth', $reginFoursAuth);
        //片区关联维护人
        $reginMaintainRules = "PianQuGuanLianWeiHuRen";
        $reginMaintainAuth = $e->check_auth($reginMaintainRules);
        $reginMaintainAuth = $reginMaintainAuth ? 1 : 0;
        $this->assign('reginMaintainAuth', $reginMaintainAuth);

        if ($params['status'] == 1) {
            $citys = $this->bc_city;
            foreach ($citys as $key => $value) {
                $city_code[] = $key;
            }
            $bc_list = I("get.bc_list");
            $params['bc_list'] = $bc_list;
            if ($bc_list > 0) {
                $address = new \AuctionCore\Other\Address();
                $cityCodes = $address->getCityByBranchId($bc_list);
                if (!empty($cityCodes)) {
                    $city_code = array_intersect($city_code, array_column($cityCodes, 'city_code'));
                }
            }
            $bc_city = I("get.bc_city");
            if ($bc_city) {
                $params['bc_city'] = $bc_city;
            } else {
                $params['bc_city'] = $city_code;
            }
            $region_id = I("get.region_id");
            if ($region_id) {
                $params['region_id'] = $region_id;
            }
            $maintain_id = I("get.maintain_id");
            if ($maintain_id) {
                $params['maintain_id'] = $maintain_id;
            }
            $params['fid'] = trim(I('get.fid'));
            $params['f_name'] = trim(I('get.f_name'));
            $params['developer_phone'] = trim(I('get.developer_phone'));
            $params['maintain_phone'] = trim(I('get.maintain_phone'));
            $result = FourStoreLogic::getFourStoreList($p, $limit, $params);
        } elseif ($params['status'] == 3) {
            $branch_id = I("get.branch_id");
            if ($branch_id) {
                $params['branch_id'] = $branch_id;
            } else {
                $params['branch_id'] = $this->user_info['branch_id'];
            }

            $params['b_name'] = trim(I('get.b_name'));
            $params['b_phone'] = trim(I('get.b_phone'));
            $params['b_contact'] = trim(I('get.b_contact'));
            $result = BlocLogic::getBlocList($p, $limit, $params);
        } elseif ($params['status'] == 4) {
            $citys = $this->bc_city;
            foreach ($citys as $key => $value) {
                $city_code[] = $key;
            }
            $bc_list = I("get.bc_list");
            $params['bc_list'] = $bc_list;
            if ($bc_list > 0) {
                $address = new \AuctionCore\Other\Address();
                $cityCodes = $address->getCityByBranchId($bc_list);
                if (!empty($cityCodes)) {
                    $city_code = array_intersect($city_code, array_column($cityCodes, 'city_code'));
                }
            }
            $bc_city = I("get.bc_city");
            if ($bc_city) {
                $params['bc_city'] = $bc_city;
            } else {
                $params['bc_city'] = $city_code;
            }
            $params['region_name'] = trim(I('get.region_name'));
            $params['leader_contact'] = trim(I('get.leader_contact'));
            $params['leader_phone'] = trim(I('get.leader_phone'));
            $result = RegionLogic::getRegionList($p, $limit, $params);
        }

        $list = empty($result['list']) ?: $result['list'];

        $this->assign('list', $list);
        $this->assign('params', $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $limit, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();
        //模板赋值
        $this->assign('page', $show);
        $this->display();
    }

    //添加4s店页面
    public function addFourStore()
    {
        $this->display();
    }

    //添加4s店提交
    public function addFourStorePost()
    {

        $fours = array();
        $FourStoreLogic = D('FourStore', 'Logic');
        $fours['f_name'] = trim(I('post.f_name'));
        $isExist = $FourStoreLogic->checkFourStoreName($fours['f_name']);
        if ($isExist) {
            $this->helpJsonResult(400, "4s店已存在");
        }
        $fours['addr'] = trim(I('post.addr'));
        $fours['contact'] = trim(I('post.contact'));
        $fours['phone'] = trim(I('post.phone'));
        $fours['developer'] = I('post.developer/d', 0);
        $fours['maintain_id'] = I('post.maintain_id/d', 0);
        $fours['city'] = trim(I('post.city/d'));
        $fours['region_id'] = trim(I('post.region_id'));
        $fours['bank_name'] = trim(I('post.bank_name'));
        $fours['card_no'] = trim(I('post.card_no'));
        $fours['account_name'] = trim(I('post.account_name'));
        //添加帐号信息
        $res = $FourStoreLogic::create($fours); //新增
        if ($res === false) {
            $this->helpJsonResult(400, "添加失败");
        }
        $this->helpJsonResult(200, "添加成功");

    }

    //修改4s店页面
    public function eidtFourStore()
    {
        $fid = I('get.fid');
        $FourStoreLogic = D('FourStore', 'Logic');
        $fourStoreInfo = $FourStoreLogic->getFourStoreInfo($fid);
        //编辑权限

        $foursTopEidtRules = "4SGaoJiXiuGai";
        $e = new Employee($this->user_info['username']);
        $foursTopEidtAuth = $e->check_auth($foursTopEidtRules);
        $foursTopEidtAuth = $foursTopEidtAuth ? 1 : 0;
        $this->assign('foursTopEidtAuth', $foursTopEidtAuth);

        //找到对应分公司下所有的开发人
        $branch_id = \AuctionCore\Other\Address::get_branch_company_by_city_code($fourStoreInfo['city']);
        $developerEmpInfo = Employee::getBranchRoleEmp($branch_id, 1);
        // //找到对应分公司下所有的维护人
        // $maintainEmpInfo = Employee::getBranchRoleEmp($fourStoreInfo['maintain_bc_id'],2);
        $RegionLogic = D('Region', 'Logic');
        $regionInfo = $RegionLogic->getRegionByCity($fourStoreInfo['city']);
        $maintainInfo = $RegionLogic->getRegionMaintain($fourStoreInfo['region_id']);
        $this->assign('developerEmpInfo', $developerEmpInfo);
        $this->assign('regionInfo', $regionInfo);
        $this->assign('maintainInfo', $maintainInfo);
        $this->assign('fourStoreInfo', $fourStoreInfo);
        $this->assign('branch_id', $branch_id);
        $this->display();
    }

    //修改4s店提交
    public function eidtFourStorePost()
    {
        $fid = I('get.fid');
        if ($fid < 1) {
            $this->helpJsonResult(400, "4s店ID有误");
        }
        $fours = array();
        $FourStoreLogic = D('FourStore', 'Logic');
        $fours['f_name'] = trim(I('post.f_name'));
        $isExist = $FourStoreLogic->checkFourStoreName($fours['f_name'], $fid);
        if ($isExist) {
            $this->helpJsonResult(400, "4s店已存在");
        }
        $fours['fid'] = $fid;
        $fours['addr'] = trim(I('post.addr'));
        $fours['contact'] = trim(I('post.contact'));
        $fours['phone'] = trim(I('post.phone'));
        $fours['developer'] = I('post.developer/d', 0);
        $fours['maintain_id'] = I('post.maintain_id/d', 0);
        $fours['region_id'] = I('post.region_id');
        $fours['city'] = trim(I('post.city/d'));
        $fours['bank_name'] = trim(I('post.bank_name'));
        $fours['card_no'] = trim(I('post.card_no'));
        $fours['account_name'] = trim(I('post.account_name'));
        $fours['account_id'] = trim(I('post.account_id'));
        $fours['channel_id'] = trim(I('post.channel_id'));
        //编辑权限
        $foursTopEidtRules = "4SGaoJiXiuGai";
        $e = new Employee($this->user_info['username']);
        $foursTopEidtAuth = $e->check_auth($foursTopEidtRules);
        if ($foursTopEidtAuth) {
            $fours['bank_name1'] = trim(I('post.bank_name1'));
            $fours['card_no1'] = trim(I('post.card_no1'));
            $fours['account_name1'] = trim(I('post.account_name1'));
            $fours['account_id1'] = trim(I('post.account_id1'));
        }
        $res = $FourStoreLogic::update($fours); //修改
        if ($res === false) {
            $this->helpJsonResult(400, "修改失败");
        }
        $this->helpJsonResult(200, "修改成功");

    }

    //4s店关联评估师页面
    public function foursRelationAppraiser()
    {
        $fid = I('get.fid/d');
        $FourStoreLogic = D('FourStore', 'Logic');
        $fourStoreInfo = $FourStoreLogic->getFourStoreInfo($fid);
        $branch_id = \AuctionCore\Other\Address::get_branch_company_by_city_code($fourStoreInfo['city']);
        //找到当前4s店下评估师
        $emp_ids = [];
        $foursAppraiser = $FourStoreLogic->getfoursAppraiser($fid);
        if (!empty($foursAppraiser)) {
            foreach ($foursAppraiser as $k => $val) {
                $foursAppraiser[$k]['count'] = $FourStoreLogic->getAppraiserFoursCount($val['emp_id']);
                $emp_ids[] = $val['emp_id'];
            }
        }
        //对应分公司下面评估师(不包含已经绑定关系评估师)
        $notinFoursAppraiser = \AuctionCore\Account\Back\Employee::getNotInFoursAppraiser($branch_id, $emp_ids);
        if (!empty($notinFoursAppraiser)) {
            foreach ($notinFoursAppraiser as $key => $value) {
                $notinFoursAppraiser[$key]['count'] = $FourStoreLogic->getAppraiserFoursCount($value['emp_id']);
            }
        }
        $this->assign('notinFoursAppraiser', $notinFoursAppraiser);
        $this->assign('foursAppraiser', $foursAppraiser);
        $this->assign('fourStoreInfo', $fourStoreInfo);
        $this->display();
    }

    //4s店关联评估师提交
    public function foursRelationAppraiserPost()
    {
        $fid = I('get.fid/d');
        if ($fid < 1) {
            $this->helpJsonResult(400, "4s店ID有误");
        }
        $appraiserIds = I('post.appraiserIds');
        $FourStoreLogic = D('FourStore', 'Logic');
        $res = $FourStoreLogic->foursRelationAppraiserPost($fid, array_filter($appraiserIds));
        if (!$res) {
            $this->helpJsonResult(400, '更新失败');
        }
        $this->helpJsonResult(200, '更新成功');
    }


    //停用启用
    public function ci_op()
    {
        $fid = I('post.fid/d');
        $status = I('post.status');
        if ($fid < 1) {
            $this->helpJsonResult(400, "4s店ID有误");
        }
        $FourStoreLogic = D('FourStore', 'Logic');
        $fourStoreInfo = $FourStoreLogic->detail($fid);
        if ($status == '1') {
            $res = $FourStoreLogic->disable($fid, $fourStoreInfo['channel_id']);
        } else if ($status === '0') {
            $res = $FourStoreLogic->enable($fid, $fourStoreInfo['channel_id']);
        } else {
            $this->helpJsonResult(401, "参数错误");
        }
        if ($res < 1) {
            $this->helpJsonResult(400, "修改失败");
        }
        $this->helpJsonResult(200, "修改成功");
    }

    public function fourStoreInfo()
    {
        $fid = I('get.fid');
        $FourStoreLogic = D('FourStore', 'Logic');
        $fourStoreInfo = $FourStoreLogic->getFourStoreInfo($fid);
        $this->assign('fourStoreInfo', $fourStoreInfo);
        $this->display();
    }

    //添加集团页面
    public function addBloc()
    {

        $this->display();
    }

    //添加集团提交
    public function addBlocPost()
    {
        $bloc = array();
        $b_name = trim(I('post.b_name'));
        $addr = trim(I('post.addr'));
        $contact = trim(I('post.contact'));
        $phone = trim(I('post.phone'));
        $branch_id = I('post.branch_id');
        $BlocLogic = D('Bloc', 'Logic');
        $isExist = $BlocLogic->checkBlocName($b_name);
        if ($isExist) {
            $this->helpJsonResult(400, "集团名称已存在");
        }
        //添加片区信息
        $bloc = [
            'b_name' => $b_name,
            'addr' => $addr,
            'contact' => $contact,
            'branch_id' => $branch_id,
            'phone' => $phone
        ];
        $res = $BlocLogic->create($bloc); //新增
        if ($res === false) {
            $this->helpJsonResult(400, "添加失败");
        }
        $this->helpJsonResult(200, "添加成功");

    }

    //修改集团页面
    public function editBloc()
    {
        $bid = I('get.bid/d');
        $BlocLogic = D('Bloc', 'Logic');
        $blocInfo = $BlocLogic->getBlocInfo($bid);
        $this->assign('blocInfo', $blocInfo);
        $this->display();
    }

    //修改集团页面
    public function editBlocPost()
    {
        $bid = I('get.bid/d');
        if ($bid < 1) {
            $this->helpJsonResult(400, "集团ID有误");
        }
        $bloc = array();
        $b_name = trim(I('post.b_name'));
        $addr = trim(I('post.addr'));
        $contact = trim(I('post.contact'));
        $phone = trim(I('post.phone'));
        $branch_id = I('post.branch_id');
        $BlocLogic = D('Bloc', 'Logic');
        $isExist = $BlocLogic->checkBlocName($b_name, $bid);
        if ($isExist) {
            $this->helpJsonResult(400, "集团名称已存在");
        }
        //添加片区信息
        $bloc = [
            'b_name' => $b_name,
            'addr' => $addr,
            'contact' => $contact,
            'branch_id' => $branch_id,
            'phone' => $phone
        ];
        $res = $BlocLogic->update($bloc, $bid); //新增
        if ($res === false) {
            $this->helpJsonResult(400, "修改失败");
        }
        $this->helpJsonResult(200, "修改成功");

    }

    //集团详情
    public function blocInfo()
    {
        $bid = I('get.bid');
        $BlocLogic = D('Bloc', 'Logic');
        $blocInfo = $BlocLogic->getBlocInfo($bid);
        $this->assign('blocInfo', $blocInfo);
        $this->display();
    }

    //集团关联4s店页面
    public function blocRelationFourStore()
    {
        $bid = I('get.bid/d');
        $BlocLogic = D('Bloc', 'Logic');
        $blocInfo = $BlocLogic->getBlocInfo($bid);
        $FourStore = D('FourStore', 'Logic');
        $notInBlocFourStore = $FourStore->getNotInBlocFourStore($blocInfo['branch_id'], $bid);
        foreach ($notInBlocFourStore as $key => $value) {
            $notInBlocFourStore[$key]['b_name'] = $BlocLogic->getBnameByBid($value['b_id']);
        }
        $this->assign('notInBlocFourStore', $notInBlocFourStore);
        $this->assign('blocInfo', $blocInfo);
        $this->display();
    }

    //集团关联4s店页面提交
    public function blocRelationFourStorePost()
    {
        $bid = I('get.bid/d');
        if ($bid < 1) {
            $this->helpJsonResult(400, "集团ID有误");
        }
        $fids = I('post.fids');
        $BlocLogic = D('Bloc', 'Logic');
        $res = $BlocLogic->blocRelationFourStorePost($bid, array_filter($fids));
        if (!$res) {
            $this->helpJsonResult(400, '更新失败');
        }
        $this->helpJsonResult(200, '更新成功');
    }

    //添加片区页面
    public function addRegion()
    {

        $this->display();
    }

    //添加片区提交
    public function addRegionPost()
    {
        $region = array();
        $region_name = trim(I('post.region_name'));
        $leader = I('post.leader/d');
        $city = I('post.city');
        $remark = trim(I('post.remark'));
        $RegionLogic = D('Region', 'Logic');
        $isExist = $RegionLogic->checkRegionName($region_name);
        if ($isExist) {
            $this->helpJsonResult(400, "片区名称已存在");
        }
        //添加片区信息
        $region = [
            'region_name' => $region_name,
            'leader' => $leader,
            'city' => $city,
            'remark' => $remark
        ];
        $res = $RegionLogic->create($region); //新增
        if ($res === false) {
            $this->helpJsonResult(400, "添加失败");
        }
        $this->helpJsonResult(200, "添加成功");
    }

    //修改片区页面
    public function editRegion()
    {
        $rid = I('get.rid/d');
        $RegionLogic = D('Region', 'Logic');
        $regionInfo = $RegionLogic->getRegionInfo($rid);
        $developerEmpInfo = Employee::getBranchEmpForSpecial(2, $regionInfo['branch_id']);
        $this->assign('developerEmpInfo', $developerEmpInfo);
        $this->assign('regionInfo', $regionInfo);
        $this->display();
    }

    //修改片区提交
    public function editRegionPost()
    {
        $rid = I('get.rid/d');
        if ($rid < 1) {
            $this->helpJsonResult(400, "片区ID有误");
        }
        $region = array();
        $region_name = trim(I('post.region_name'));
        $leader = I('post.leader/d');
        $city = I('post.city');
        $remark = trim(I('post.remark'));
        $RegionLogic = D('Region', 'Logic');
        $isExist = $RegionLogic->checkRegionName($region_name, $rid);
        if ($isExist) {
            $this->helpJsonResult(400, "片区名称已存在");
        }
        //添加片区信息
        $region = [
            'region_name' => $region_name,
            'leader' => $leader,
            'city' => $city,
            'remark' => $remark
        ];
        $res = $RegionLogic->update($region, $rid); //新增
        if ($res === false) {
            $this->helpJsonResult(400, "修改失败");
        }
        $this->helpJsonResult(200, "修改成功");

    }

    //获取片区信息
    public function RegionInfo()
    {
        $rid = I('get.rid/d');
        $RegionLogic = D('Region', 'Logic');
        $regionInfo = $RegionLogic->getRegionInfo($rid);
        $this->assign('regionInfo', $regionInfo);
        $this->display();
    }

    //通过分公司找维护人 维护人电话 城市 
    public function getBranchInfo()
    {
        $branch_id = I("post.branch_id", 0);
        if ($branch_id < 1) {
            $this->helpJsonResult(400, '分公司信息错误');
        }
        $shop_developer = Employee::getBranchEmpForSpecial(2, $branch_id);
        $address = new \AuctionCore\Other\Address();
        $cityInfo = $address->getCityByBranchId($branch_id);
        $result = [];
        $result['cityInfo'] = $cityInfo ? $cityInfo : [];
        $result['shop_developer'] = $shop_developer ? $shop_developer : [];
        $this->helpJsonResult(200, '', $result);
    }

    // //通过分公司找(没有绑定片区)开发人 开发人电话 城市 
    // public function getBranchEmpNotRegion()
    // {
    //     $branch_id = I("post.branch_id", 0);
    //     if ($branch_id < 1) {
    //         $this->helpJsonResult(400, '分公司信息错误');
    //     }
    //     // $shop_developer_id = C('SHOP_DEVELOPER.0');
    //     $shop_developer = Employee::getBranchEmpForSpecial(1,$branch_id);
    //     $address = new \AuctionCore\Other\Address();
    //     $cityInfo = $address ->getCityByBranchId($branch_id);
    //     $result = [];
    //     $result['cityInfo'] = $cityInfo ? $cityInfo : [];
    //     $result['shop_developer'] = $shop_developer ? $shop_developer : [];
    //     $this->helpJsonResult(200, '', $result);
    // }


    //通过分公司找开发人 维护人
    public function getBranchEmp()
    {
        $branch_id = I("post.branch_id", 0);
        if ($branch_id < 1) {
            $this->helpJsonResult(400, 'empty');
        }
        $isFlag = I("post.is_flag/d", 1);
        $empInfo = Employee::getBranchEmpForSpecial($isFlag, $branch_id);
        if (empty($empInfo)) {
            $this->helpJsonResult(400, 'empty', []);
        }
        $this->helpJsonResult(200, '', $empInfo);
    }


    //片区管理- 关联店铺
    public function regionRelationFourStore()
    {
        $rid = I('get.rid/d');
        $RegionLogic = D('Region', 'Logic');
        $regionInfo = $RegionLogic->getRegionInfo($rid);
        $this->assign('regionInfo', $regionInfo);
        //对应分公司下不在当前片区下的4s店
        $fourStore = D('FourStore', 'Logic');
        $branch_id = \AuctionCore\Other\Address::get_branch_company_by_city_code($regionInfo['city']);
        $address = new \AuctionCore\Other\Address();
        $city = $address->get_city_by_companyid($branch_id);
        $notInFourStore = $fourStore->getNotInFourStoreByCity($city, $rid);
        foreach ($notInFourStore as $key => $value) {
            if ($value['region_id'] > 0) {
                $notInFourStore[$key]['region_name'] = $RegionLogic->getRegionNameByRid($value['region_id']);
            }
        }
        //找到当前片区下4s店城市
        $InFourStore = $fourStore->getInFourStoreByRid($rid);
        $this->assign('InFourStore', $InFourStore);
        $this->assign('notInFourStore', $notInFourStore);
        $this->display();
    }

    //片区管理- 关联店铺提交
    public function regionRelationFourStorePost()
    {
        $rid = I('get.rid/d');
        if ($rid < 1) {
            $this->helpJsonResult(400, "片区ID有误");
        }
        $fids = I('post.fids');
        $RegionLogic = D('Region', 'Logic');
        $res = $RegionLogic->regionRelationFourStorePost($rid, array_filter($fids));
        if (!$res) {
            $this->helpJsonResult(400, '更新失败');
        }
        $this->helpJsonResult(200, '更新成功');
    }

    //片区管理 - 关联维护人 
    public function regionRelationMaintain()
    {
        $rid = I('get.rid/d');
        $RegionLogic = D('Region', 'Logic');
        $fourStore = D('FourStore', 'Logic');
        $regionInfo = $RegionLogic->detail($rid);
        $branch_id = \AuctionCore\Other\Address::get_branch_company_by_city_code($regionInfo['city']);
        //对应分公司下面维护人（不在当前片区）
        $notInMaintain = \AuctionCore\Account\Back\Employee::getNotInRegionEmpInfo($branch_id, $rid);
        if (!empty($notInMaintain)) {
            foreach ($notInMaintain as $key => $value) {
                $notInMaintain[$key]['count'] = $fourStore->getFourStoreCount($value['emp_id']);
                if ($value['region_id'] > 0) {
                    $selfRegion = $RegionLogic->detail($value['region_id']);
                    $notInMaintain[$key]['region_name'] = $selfRegion['region_name'];
                }
            }
        }
        //找到当前片区下维护人
        $inMaintain = \AuctionCore\Account\Back\Employee::getInRegionMaintain($rid);
        if (!empty($inMaintain)) {
            foreach ($inMaintain as $k => $val) {
                $inMaintain[$k]['count'] = $fourStore->getFourStoreCount($val['emp_id']);
            }
        }
        $this->assign('regionInfo', $regionInfo);
        $this->assign('notInMaintain', $notInMaintain);
        $this->assign('inMaintain', $inMaintain);
        $this->display();
    }

    //片区管理 - 关联维护人 
    public function regionRelationMaintainPost()
    {
        $rid = I('get.rid/d');
        if ($rid < 1) {
            $this->helpJsonResult(400, "片区ID有误");
        }
        $maintainIds = I('post.maintainIds');
        $RegionLogic = D('Region', 'Logic');
        $res = $RegionLogic->regionRelationMaintainPost($rid, array_filter($maintainIds));
        if (!$res) {
            $this->helpJsonResult(400, '更新失败');
        }
        $this->helpJsonResult(200, '更新成功');
    }

    //通过分公司找城市
    public function getCityByBranchId()
    {
        $branch_id = I("post.branch_id", 0);
        if ($branch_id < 1) {
            $this->helpJsonResult(400, 'empty');
        }
        $cityInfo = \AuctionCore\Other\Address::getCityByBranchId($branch_id);
        if (empty($cityInfo)) {
            $this->helpJsonResult(400, 'empty');
        }
        $this->helpJsonResult(200, '', $cityInfo);
    }

    /**
     *找到城市下的片区
     */
    public function getRegionByCity()
    {
        $city = I("post.city");
        if ($city < 1) {
            $this->helpJsonResult(400, 'empty');
        }
        $RegionLogic = D('Region', 'Logic');
        $regionInfo = $RegionLogic->getRegionByCity($city);
        if (empty($regionInfo)) {
            $this->helpJsonResult(400, 'empty');
        }
        $this->helpJsonResult(200, '', $regionInfo);
    }

    /**
     *找到片区下的维护人
     */
    public function getMaintainByRegionId()
    {
        $region_id = I("post.region_id/d");
        if ($region_id < 1) {
            $this->helpJsonResult(400, 'empty');
        }
        $RegionLogic = D('Region', 'Logic');
        $maintainInfo = $RegionLogic->getRegionMaintain($region_id);
        if (empty($maintainInfo)) {
            $this->helpJsonResult(400, 'empty');
        }
        $this->helpJsonResult(200, '', $maintainInfo);
    }

}
